<template>
  <a-modal
    :title="(model&&model.id) ? '编辑': '新增'"
    :width="660"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleOK"
    @cancel="() => { $emit('cancel') }">
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item label="id" style="display: none;">
          <a-input v-decorator="['id']" />
        </a-form-item>
        
        <a-form-item label="名称">
          <a-input placeholder="请输入:名称" v-decorator="['name', { rules: [{ required: true, message: '请输入：名称' }] }]" />
        </a-form-item>
        
        <a-form-item label="权限">
          <a-input placeholder="请输入:权限" v-decorator="['perms', { rules: [{ required: true, message: '请输入：权限' }] }]" />
        </a-form-item>
        
        <a-form-item label="类型">
          <a-select v-decorator="['type', { rules: [{ required: true, message: '请选择：类型' }] }]" placeholder="请选择：类型">
            <a-select-option :value="1">目录</a-select-option>
            <a-select-option :value="2">菜单</a-select-option>
          </a-select>
        </a-form-item>
        
        <a-form-item label="上级目录">          
          <a-select v-decorator="['pid', { rules: [{ required: true, message: '请选择：父级目录' }] }]" placeholder="请选择：父级目录">
            <a-select-option v-for="item in dirList" :value="item.id">{{ item.name }}</a-select-option>
          </a-select>          
        </a-form-item>
        
        <a-form-item label="菜单URL">
          <a-input v-decorator="['url']" placeholder="请输入:菜单URL" />
        </a-form-item>
        
        <a-form-item label="排序号">
          <a-input placeholder="请输入:排序号" v-decorator="['orderNum', { rules: [{ required: true, message: '请输入：排序号' }] }]" />
        </a-form-item>
        
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import locale from 'ant-design-vue/es/date-picker/locale/zh_CN'
import moment from 'moment'
// 表单字段
const fields = ['id', 'name', 'perms', 'orderNum', 'type', 'pid', 'url']
// const baseApi = process.env.VUE_APP_API_BASE_URL
// import UploadPreview from '@/components/upload-prevew'
import { listDir } from '@/api/sys.js'
export default {
  components: { },
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 }
      }
    }
    return {
      form: this.$form.createForm(this),
      locale: locale,
      dirList: []
    }
  },
  created () {
    
    // 加载目录菜单，2024年10月28日20:01:17
    this.listDir()
    
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))
    
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      if (this.model) {
        this.form.setFieldsValue(pick(this.model, fields))
      }
    })
  },
  methods: {
    handleOK () {
      this.$emit('ok')
    },
    listDir() {
      listDir().then(res => {
      this.dirList = res.data
      })
    }
  }
}
</script>
<style scoped lang="less">
.avatar-uploader > .ant-upload {
  width: 128px;
  height: 128px;
}
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
.upload-wrapper {
  display: flex;
}
.img-list {
  display: flex;
  .img-item {
    position: relative;
    width: 100px;
    height: 100px;
    padding: 4px;
    overflow: hidden;
    box-sizing: border-box;
    margin-right: 10px;
    border-radius: 10px;
    overflow: hidden;
    &:hover .img-list .img-item .mask{
      display: block;
    }
    .img {
      height: 100%;
      width: 100%;
    }
  }
  .upload-btn {
    width: 100px;
    height: 100px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

  }
  .myicon {
    font-size: 20px;
    color: #fff;
    &:hover {
      color: #1890ff;
    }
  }
  .mask {
    display: none;
    position: absolute;
    height: 100%;
    width: 100%;
    background-color: rgba(0, 0, 0, .65);
    left: 0;
    top: 0;
    display: flex;
    align-items: center;
    justify-content: space-around;
  }
}
.notice-text {
  color: #f5222d;
}

</style>
